package com.gs.synedocket.signalr;

import android.util.Log;

import com.gs.synedocket.callback.SignalRCallBack;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.util.SPUtil;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.HubConnectionState;

public class SignalRConn {

    private static final String TAG = "SignalR";
    private SignalRCallBack signalRCallBack;
    private HubConnection hubConnection;
    private String webURl;
    private long serverTimeout = 30000;
    private long keepAliveInterval = 30000;

    public SignalRConn(String webURl, SignalRCallBack signalRCallBack) {
        this.webURl = webURl;
        this.signalRCallBack = signalRCallBack;

        hubConnection = HubConnectionBuilder.create(webURl)
                .build();
        hubConnection.on(SignalRCode.RECEIVE_MSG, (s, s1) -> {
            Log.i(AllConstants.TAG, s + s1);
            if (signalRCallBack != null) {
                signalRCallBack.receiveMessage(s, s1);
            }

        }, String.class, String.class);
        hubConnection.setServerTimeout(serverTimeout);
        hubConnection.setKeepAliveInterval(serverTimeout);
        hubConnection.onClosed(exception -> {
            if (signalRCallBack != null) {
                Log.i(TAG, "closed: ");
                signalRCallBack.signalRClosed();
            }
        });
    }

    public SignalRConn(String webURl, long serverTimeout, long keepAliveInterval, SignalRCallBack signalRCallBack) {
        this.webURl = webURl;
        this.signalRCallBack = signalRCallBack;
        this.serverTimeout = serverTimeout;
        this.keepAliveInterval = keepAliveInterval;
        hubConnection = HubConnectionBuilder.create(webURl)
                .build();
        hubConnection.on(SignalRCode.RECEIVE_MSG, (s, s1) -> {
            if (signalRCallBack != null) {
                signalRCallBack.receiveMessage(s, s1);
            }

        }, String.class, String.class);
        hubConnection.setServerTimeout(serverTimeout);
        hubConnection.setKeepAliveInterval(serverTimeout);
        hubConnection.onClosed(exception -> {
            if (signalRCallBack != null) {
                Log.i(TAG, "closed: ");
                signalRCallBack.signalRClosed();
            }
        });
    }
    /*
    public SignalR() {

    }

    public SignalR setWebURl(String webURl) {
        this.webURl = webURl;
        return this;
    }

    public SignalR setLogin(LoginBean login) {
        this.login = login;
        return this;
    }

    public SignalR setSignalRCallBack(SignalRCallBack signalRCallBack) {
        this.signalRCallBack = signalRCallBack;
        return this;
    }

     */

    public void start() throws SignalRException {
        try {
            Log.i(TAG, "start: ");
            hubConnection.start().blockingAwait();
            if (hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED) {
                throw new SignalRException();
            }
            hubConnection.send(SignalRCode.LOGIN, SPUtil.getInstance().getString(AllConstants.ID));
        } catch (Exception e) {
            e.printStackTrace();
            throw new SignalRException();
        }
    }

    /**
     * 关闭连接
     */
    public void stopConnect() {
        try {
            if (hubConnection != null) {
                hubConnection.stop();
                hubConnection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
